<template>
  <div class="main">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="100px">
      <div class="search">
        <el-form-item label="項目名称" prop="name">
          <el-input v-model.trim="queryParams.name" placeholder="请输入項目名称搜索" clearable/>
        </el-form-item>
        <el-form-item label="項目描述" prop="description">
          <el-input v-model.trim="queryParams.description" placeholder="请输入項目描述搜索" clearable/>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择" clearable>
            <el-option v-for="dict in statusList" :key="dict.value" :label="dict.text"
                       :value="dict.value"/>
          </el-select>
        </el-form-item>
        <el-form-item label="创建人" prop="createUser">
          <el-input v-model.trim="queryParams.createUser" placeholder="请输入创建人名称搜索" clearable/>
        </el-form-item>
      </div>
      <div class="search">
        <el-button type="primary" @click="restList(query.keywords)" icon="el-icon-search">查询</el-button>
        <el-button @click="refresh('queryForm')" icon="el-icon-refresh">重置</el-button>
      </div>
    </el-form>
    <div class="create-btn">
      <el-button type="primary" icon="el-icon-plus" @click="openDialog('新建项目')" v-permission="'project:add'">新建项目</el-button>
    </div>
    <div class="create-btn">
      <el-table :data="tableData" element-loading-spinner="el-icon-loading" element-loading-text="加载中。。。"
                element-loading-custom-class="equipment-loading" element-loading-background="rgba(55,55,55,0.2)"
                max-height="550">
        <el-table-column fixed prop="name" label="項目名称"></el-table-column>
        <el-table-column prop="description" label="描述"></el-table-column>
        <el-table-column prop="status" label="状态" width="120">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status === '1' ? true : false"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="createUser" label="创建人" width="120"></el-table-column>
        <!--        <el-table-column prop="updateUser" label="更新人" width="120"></el-table-column>-->
        <!--        <el-table-column prop="createTime" label="创建时间" width="150"></el-table-column>-->
        <el-table-column prop="updateTime" label="更新时间" width="150"></el-table-column>
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button @click="openDialog('编辑项目',scope.row)" type="text" v-permission="'project:update'">编辑</el-button>
            <el-popconfirm
              @onConfirm="handleDelete(scope.row)"
              confirm-button-text='确定'
              cancel-button-text='我再看看'
              icon="el-icon-info"
              icon-color="red"
              title="是否确认删除？"
            >
              <el-button slot="reference" type="text" v-permission="'project:delete'">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="query.pageNum"
        :limit.sync="query.pageRow"
        layout="total, prev, pager, next"
        :hide-on-single-page="value"
        @pagination="getProjectList"
      />
    </div>
    <cu-dialog :dialogVisble="visible" :title="title" :editData="editData" @closeDialog="closeDialog"
               @refreshList="restList"></cu-dialog>
  </div>
</template>

<script>
import Pagination from "@/components/Pagination/index";
import CuDialog from "./cuDialog";

export default {
  name: "project",
  components: {Pagination, CuDialog},
  data() {
    return {
      queryParams: {
        name: "",
        status: "",
        createUser: "",
        description: ""
      },
      statusList: [
        {
          text: "开",
          value: 1
        },
        {
          text: "关",
          value: 0
        },
      ],
      tableData: [],
      query: {
        pageRow: 10,
        pageNum: 1
      },
      value: false,
      total: 0,
      visible: false,
      title: "",
      editData: {}
    };
  },
  mounted() {
    this.getProjectList();
  },
  methods: {
    refresh(queryForm) {
      this.$refs[queryForm].resetFields();
      this.getProjectList();
    },
    restList() {
      this.query.pageNum = 1;
      this.getProjectList();
      this.closeDialog()
    },
    getProjectList() {
      this.query = {...this.query, ...this.queryParams}
      this.api({
        url: "/getProjectList",
        method: "post",
        data: this.query
      }).then(resp => {
        this.tableData = resp.list;
        this.total = resp.totalCount;
      });
    },
    openDialog(title, data = null) {
      this.title = title;
      this.visible = true;
      this.editData = data ? data : {}
    },
    closeDialog() {
      this.visible = false;
    },
    handleDelete(data) {
      this.api({url: `/deleteProject/${data.id}`, method: "delete"}).then((resp) => {
        this.getProjectList()
        this.$message({message: resp, type: 'success'});
      })
    },
  }
};
</script>

<style scoped lang="scss">
.main {
  height: calc(100vh - 110px);
  padding: 20px;
  background-color: white;
  margin: 20px;
}

.search {
  margin: auto;
  text-align: center
}

.create-btn {
  margin-top: 10px;
}
</style>
